/**
 * Created by zoushuiyun on 2017/7/25.
 */
import { Injectable } from '@angular/core';
import {
  CanActivate, Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
} from '@angular/router';
import { CacheService } from '@core/routes-services/cache.service';
import { MyModalHelper } from '@core/my-modal.helper';
import { TriggerLoginComponent } from '../../routes/market-trigger/trigger-login/trigger-login.component';
import { NzMessageService } from 'ng-zorro-antd';

/**
 * 奖金发放器页面路由守卫
 */
@Injectable()
export class TriggerAuthGuardActivate implements CanActivate {
  constructor(private cacheService: CacheService,
              public message: NzMessageService,
              private modalHelper: MyModalHelper, private router: Router) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> | boolean {
    const url: string = state.url;

    return this.checkLogin(url);
  }

  /**
   * 奖金发放器页面守卫
   *
   * @param url
   * @returns {boolean}
   */
  checkLogin(url: string): Promise<boolean> {
    let that = this;
    return new Promise((resolve, reject) => {
      that.modalHelper.open(TriggerLoginComponent, {}, 600, {
        nzTitle: '操作校验',
      }).subscribe(result => {
        if (result) {
          resolve(true);
        } else {
          that.message.warning('操作验证失败');
          resolve(false);
        }
      }, (error) => {
        that.message.warning('操作验证失败.');
        resolve(false);
      });
    });
  }

}
